<?php

include_once($ROOT . "base.inc.php");

class PageController extends BaseController {
  var $user;
  var $oldPassword;
  var $newPassword1;
  var $newPassword2;

  function PageController() {
	parent::__construct();

	$this->title="Change Password";
	$this->section="home";

	$action = $this->getParameter("action");
	$this->user = UserUtil::getCurrentUser();


	switch (strtoupper($action))
	{
		case "CHANGE PASSWORD" :
			  $this->parseForm();
			  if (!$this->validate()) break;;

			  $this->user->password = $this->newPassword1;
			  if (!UserLogic::save($this->user))
			  {
			  	  $this->addError("Could not change password!");
			  	  break;
			  }

			  $this->addMessage("Password Changed.");
			  break;
		case "CANCEL" :
			  HtmlUtil::redirectToPage("index");
			  break;

	}

  }

  function parseForm() {
  	$this->oldPassword = $this->getParameter("oldPassword");
  	$this->newPassword1 = $this->getParameter("newPassword1");
  	$this->newPassword2 = $this->getParameter("newPassword2");
  }

  function validate() {
  	if (!UserUtil::authenticate($this->user->username, $this->oldPassword))
  		$this->addError("Incorrect current password");
  	if ($this->newPassword1 != $this->newPassword2)
  		$this->addError("Passwords do not match!");
	return (sizeof($this->errs) == 0);
  }

}



?>
